package com.masterclient.app.modules.order.contract;

import android.text.TextUtils;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.order.ConfirmOrderData;
import com.eoner.baselibrary.bean.order.ConfirmOrderOrder;
import com.eoner.baselibrary.bean.order.OrderProductMessage;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.eoner.baselibrary.bean.order.ConfirmOrderBean;
import com.eoner.baselibrary.bean.order.RefreshOrderBean;
import com.eoner.baselibrary.bean.order.CreateOrderBean;
import com.eoner.baselibrary.bean.order.TradeStatusBean;
import org.json.JSONException;
import org.json.JSONObject;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class SubmitOrderPresenter extends SubmitOrderContract.Presenter{



    @Override
    public void confirm_order(String buy_product, String buy_type, String charge_account,String sale_type,String relation_order_id,String is_custody) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (buy_product!=null) {
                JSONObject buyProduct = new JSONObject(buy_product);
                jsonObject.put("buy_product",buyProduct);
            }
            jsonObject.put("buy_type",Integer.parseInt(buy_type));
            jsonObject.put("sale_type",sale_type);
            jsonObject.put("charge_account",charge_account);
            jsonObject.put("relation_order_id",relation_order_id);
            jsonObject.put("is_custody",is_custody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ConfirmOrderBean> observable = RetrofitUtil2.getInstance().confirmOrder(body);
        Disposable disposable = toSubscribe(observable, new Consumer<ConfirmOrderBean>() {
            @Override
            public void accept(@NonNull ConfirmOrderBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.confirm_order_success(bean.getData());
                }else{
                    mView.confirm_order_failed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.confirm_order_failed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void refresh_order(String buy_product,String buy_type,String coupon_id,String address_id,
                              String customs_clearance_id,String bupiao,String balance,String gold,String sale_type,
                              String freight_type,String relation_order_id,String is_custody) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (buy_product!=null) {
                JSONObject buyProduct = new JSONObject(buy_product);
                jsonObject.put("buy_product", buyProduct);
            }
            jsonObject.put("buy_type",Integer.parseInt(buy_type));
            jsonObject.put("coupon_id",Integer.parseInt(coupon_id));
            jsonObject.put("address_id",Integer.parseInt(address_id));
            jsonObject.put("customs_clearance_id",customs_clearance_id);
            jsonObject.put("bupiao",Integer.parseInt(bupiao));
            jsonObject.put("balance",Integer.parseInt(balance));
            jsonObject.put("gold",Integer.parseInt(gold));
            jsonObject.put("freight_type",Integer.parseInt(freight_type));
            jsonObject.put("relation_order_id",relation_order_id);
            jsonObject.put("is_custody",is_custody);
            jsonObject.put("sale_type",sale_type);
            jsonObject.put("relation_order_id",relation_order_id);
            jsonObject.put("is_custody",is_custody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<RefreshOrderBean> observable = RetrofitUtil2.getInstance().refreshOrder(body);
        Disposable disposable = toSubscribe(observable, new Consumer<RefreshOrderBean>() {
            @Override
            public void accept(@NonNull RefreshOrderBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.refresh_order_success(bean.getData());
                }else{
                    mView.refresh_order_failed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.refresh_order_failed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void create_order(String live_room_id,String buy_product, String buy_type, String coupon_id, String payment_id, String bupiao,
                             String balance, String gold, String address_id, String customs_clearance_id,
                             String customs_clearance_agreement, String remark, String invoice, String charge_account,String sale_type,
                             String freight_type,String relation_order_id,String is_custody){
        JSONObject jsonObject = new JSONObject();
        try {
            if (buy_product!=null) {
                JSONObject buyProduct = new JSONObject(buy_product);
                jsonObject.put("buy_product", buyProduct);
            }
            jsonObject.put("live_room_id",live_room_id);
            jsonObject.put("buy_type",Integer.parseInt(buy_type));
            jsonObject.put("coupon_id",Integer.parseInt(coupon_id));
            jsonObject.put("payment_id",Integer.parseInt(payment_id));
            jsonObject.put("bupiao",Integer.parseInt(bupiao));
            jsonObject.put("balance",Integer.parseInt(balance));
            jsonObject.put("gold",Integer.parseInt(gold));
            jsonObject.put("address_id",Integer.parseInt(address_id));
            jsonObject.put("customs_clearance_id",Integer.parseInt(customs_clearance_id));
            jsonObject.put("customs_clearance_agreement",customs_clearance_agreement);
            jsonObject.put("charge_account",charge_account);
            jsonObject.put("sale_type",sale_type);
            jsonObject.put("freight_type",Integer.parseInt(freight_type));
            jsonObject.put("relation_order_id",relation_order_id);
            jsonObject.put("is_custody",is_custody);
            if (!TextUtils.isEmpty(remark)){
                JSONObject remarkJson = new JSONObject(remark);
                jsonObject.put("remark",remarkJson);
            }
            if (!TextUtils.isEmpty(invoice)){
                JSONObject invoiceJson = new JSONObject(invoice);
                jsonObject.put("invoice",invoiceJson);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CreateOrderBean> observable = RetrofitUtil2.getInstance().createOrder(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CreateOrderBean>() {
            @Override
            public void accept(@NonNull CreateOrderBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.create_order_success(bean.getData());
                }else{
                    mView.create_order_failed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.create_order_failed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void get_trade_status(String trade_no) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("trade_no",trade_no);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<TradeStatusBean> observable = RetrofitUtil2.getInstance().get_trade_status(body);
        Disposable disposable = toSubscribe(observable, new Consumer<TradeStatusBean>() {
            @Override
            public void accept(@NonNull TradeStatusBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.get_trade_status_success(bean.getData());
                }else{
                    mView.get_trade_status_failed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.get_trade_status_failed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    /**
    * 判断是否是赠送品
    * */
    public boolean checkIsGiving(ConfirmOrderData data) {
        if (data.getOrders() != null && data.getOrders().size() > 0) {
            for (ConfirmOrderOrder order:data.getOrders()) {
                if (order.getProducts() != null && order.getProducts().size() > 0) {
                    for (OrderProductMessage productMessage:order.getProducts()) {
                        if (productMessage.getEvent_type()!=null && productMessage.getEvent_type().equals("3")) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
}